<?php
/**
 * AdminAction.class.php    系统管理员操作类.php
 *
 * @author                  mashanling(msl-138@163.com)
 * @date                    2012-03-21 14:36:19
 * @last modify             2012-03-21 14:36:19 by mashanling
 */

!defined('ROOT') && exit('Access Denied');

class AdminAction extends CommonAction {
    function index() {
        $smarty = $this->getView('smarty');
        //$smarty && $smarty->caching = true;

        if (!$this->isCached()) {
            $db   = D('Admin');
            $data = $db->getList();
            $this->assign(array('TITLE' => 'list_admin', 'data' => $data));
            $model = D('Group');
            $group = $model->getOption();
            $this->assign('group', $group);
            $this->display();
        }
        else {
            $this->cachedDisplay();
        }

    }

    function admin_list() {
        //C('TMPL_ENGINE_TYPE', 'php');
        $this->index();
    }

    function add() {
        $title = '添加';
        $id    = Param::get('id', 'int');

        if ($id && ($data  = D('Admin')->getInfo($id))) {
            $this->assign('data', $data);
            $title = '编辑';
        }

        $model = D('Group');
        $group = $model->getOption();
        $this->assign('group', $group)->assign('TITLE', $title . '管理员');
        $this->display();
    }

    function save() {
        $model = D('Admin');
        $check = $model->create();
        $success = false;

        unset($model->sa_pswd_confirm);

        if (!$check) {
            $msg = join('<br />', $model->getError());
        }
        elseif ($model->said) {

            if ($model->sa_pswd == '') {
                unset($model->sa_pswd);
            }
            else {
                $model->sa_pswd = md5($model->sa_pswd);
            }

            if ($model->save() === false) {
                $msg = '编辑失败';
            }
            else {
                $success = true;
                $msg = '编辑成功';
            }
        }
        else {
            if ($model->add() === false) {
                $msg = '添加失败';
            }
            else {
                $success = true;
                $msg = '添加成功';
            }
        }

        !$success && !$model->getError() && Log::write("添加/编辑管理员{$model->said}失败." . $model->getDbError());
        $this->ajaxReturn(array('success' => $success, 'msg' => $msg));
    }//end save

    function delete() {
        $id     = Param::get('id', 'int');
        $model  = D('Admin');
        if ($id && $model->delete($id) !== false) {
            $result = '删除成功';
        }
        else {
            $result = '删除失败';
            ($error = $model->getDbError()) && Log::write("删除管理失败{$id}失败.错误信息: {$error}", Log::SQL);
        }
        cookie('__msg__', $result);
        redirect($_SERVER['HTTP_REFERER']);
    }

    function clear() {
        $id     = Param::get('id', 'int');
        $model  = D('Admin');
        if ($id && $model->where('said=' . $id)->setField('is_check_hd', 0) !== false) {
            $result = '清理授权成功';
        }
        else {
            $result = '清理授权失败';
            ($error = $model->getDbError()) && Log::write("清理授权管理失败{$id}失败.错误信息: {$error}", Log::SQL);
        }
        cookie('__msg__', $result);
        redirect($_SERVER['HTTP_REFERER']);
    }
}